package com.hu.bgp.server;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.hu.bgp.shared.AdminService;
import com.hu.bgp.shared.ClientGameType;
import com.hu.bgp.shared.ClientGameTypeCollection;

/**
 * De 'admin' servlet waarmee game geactiveerd en gedeactiveerd kunnen worden.
 * 
 * @author ejvos
 */
public class AdminServiceImpl extends RemoteServiceServlet implements AdminService
{
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		System.out.println("AdminServiceImpl.init");
		GameServerContext gsc = GameServerContext.getInstance(getServletContext());
	}

	public ClientGameTypeCollection getClientGameTypeAdminCollection()
	{
		System.out.println("AdminServiceImpl.getClientGameTypeAdminCollection");
		GameServerContext gsc = GameServerContext.getInstance(getServletContext());

		ClientGameTypeCollection result = new ClientGameTypeCollection();
		for (GameType gt : gsc.getGameTypeCollection())
		{
			int playerCount = gt.getGameInstances().countPlayers();
			result.add(new ClientGameType(gt.getAlias(), gt.getDisplayName(), gt.getDescription(), gt.isActive(), playerCount));
		}

		return result;
	}

	public void setGameTypeActivation(ClientGameType gameType, boolean activate)
	{
		System.out.println("AdminServiceImpl.activateGame");
		GameServerContext gsc = GameServerContext.getInstance(getServletContext());
		GameType gt = gsc.getGameTypeCollection().getByAlias(gameType.getAlias());
		if (!gt.isActive() && activate)
		{
			gt.activate();
		}
		else if (gt.isActive() && !activate)
		{
			gt.deactivate();
		}
	}
}
